<?php
if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Media extends CI_Controller
{
    
    function __construct() {
        parent::__construct();
        $this->load->library('breadcrumb');
    }
    
    function index() {
        
        //rule type pada function ini: Read
        $this->rule->type('R');
        
        //title
        $this->layout->set_title('Pustaka Media');
        
        //breadcrumb/untuk navigasi
        $this->breadcrumb->clear();
        $this->breadcrumb->add_crumb('Dashboard', site_url('admin'));
        $this->breadcrumb->add_crumb('Media', site_url('media#'));
        $this->breadcrumb->add_crumb('Pustaka Media');
        
        //load model
        $this->load->model('m_media');

        //paging dan ambil data dari model
        $this->load->library('paging');
        $config['sql'] = $this->m_media->get();
        $config['per_page'] = 15;
        $data = $this->paging->create_pagination($config);
        
        //judul besar
        $data['primary_title'] = '<i class="fa fa-camera"></i> Media';
        $data['sub_primary_title'] = 'Pengaturan media Notoboso';
        
        //jquery form
         $this->layout->set_include('js', 'themes/back/bundle/jquery.form/jquery.form.min.js', 'header');

        //jquery untuk auosave
        $this->layout->set_include('js', 'themes/back/bundle/jquery.autosave/jquery.autosave.js','header');


        //menghilangkan session pencarian jika ada
        $this->session->unset_userdata('keyword');
        
        //untuk crop dan atau resize image
        $this->load->helper('image');

        //untuk mengetahui ukuran file
        $this->load->helper('get_filesize');

        //untuk tanggal indonesia
        $this->load->helper('id_date');

        //menggunakan theme back
        $this->layout->theme('back', 'media/index', $data);
    }

    function detail($id='') {
        //rule type pada function ini: Update
        $this->rule->type('U');
        
        //untuk tanggal indonesia
        $this->load->helper('id_date');

        //untuk crop dan atau resize image
        $this->load->helper('image');

        //untuk mengetahui ukuran file
        $this->load->helper('get_filesize');

        $this->load->model('m_media');
        $data['results'] = $this->m_media->get_by($id);
        $this->load->view('back/layouts/media/detail', $data);
    }

    function update(){
        if (isset($_POST) && !empty($_POST)) {
            
            //ambil data dari form
            $id_media = $this->input->post('id_media');
            $dataUpdate = array(
                'title' => $this->input->post('title'),
                'caption' => $this->input->post('caption'),
                'alt_text' => $this->input->post('alt_text'),
                'desc' => $this->input->post('desc')
            );
            
            $this->load->model('m_media');
            $this->m_media->update($id_media, $dataUpdate);

        } else {
            show_404();
        }
    }

    function image_upload(){
        if (isset($_POST)) {
            $this->load->helper('slug');
            $upload_path            = './uploads/';
            $base_name              = $_FILES['image_file']['name'];
            $original_source_path   = $upload_path . $base_name;
            $file_name              = pathinfo($original_source_path, PATHINFO_FILENAME ); 
            //untuk jadiin slug       
            $new_name               = convert_to_slug($file_name);

            $config['upload_path'] = $upload_path;
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '2000';
            $config['max_width']  = '2048';
            $config['max_height']  = '1536';
            $config['file_name'] = $new_name;

            $this->load->library('upload', $config);

            //untuk crop dan atau resize image
            $this->load->helper('image');

            if (!$this->upload->do_upload('image_file')){
                $data['error']          = $this->upload->display_errors();
                $this->load->view('back/layouts/media/image_upload', $data);
            }else{
                $upload_data            = $this->upload->data();
                $data['upload_data']    = $upload_data;

                $dataInsert = array(
                  'file_name' => $upload_data['file_name'],
                  'file_type' => $upload_data['file_type'],
                  'datetime' => date('Y-m-d H:i:s'),
                  'title' => $file_name,
                  'alt_text' => $file_name,
                  'user_id' => $this->ion_auth->user()->row()->id
                );

                //load model
                $this->load->model('m_media');
                //simpan, jika tidak kesimpan di database maka file dihapus lagi dari server
                if (!$this->m_media->save($dataInsert)) {
                    unlink($upload_path . $upload_data['file_name']);
                }

                $this->load->view('back/layouts/media/image_upload', $data);
            }
        }else{
            show_404();
        }
    }

    function image_crop() {
        if (isset($_POST) && !empty($_POST)) {

            $full_source_path           = $this->input->post('full_source_path');
            $upload_path                = './uploads/';
            $file_name                  = pathinfo($full_source_path, PATHINFO_FILENAME );
            $file_extension             = pathinfo($full_source_path, PATHINFO_EXTENSION );
            $x_axis                     = $this->input->post('dataX');
            $y_axis                     = $this->input->post('dataY');
            $orig_width                 = $this->input->post('dataWidth');
            $orig_height                = $this->input->post('dataHeight');
            
            $this->load->library('image_lib');

            $config['image_library']    = 'gd2';
            //source file yang akan di crop
            $config['source_image']     = $full_source_path;
            //jarak dari sumbu x dan y (atas dan bawah)
            $config['x_axis']           = $x_axis;
            $config['y_axis']           = $y_axis;
            $config['maintain_ratio']   = FALSE;
            //ukuran yang diinginkan setelah di crop
            $config['width']            = 250;
            $config['height']           = 250;
            //ukuran gambar sesuai area/kordinat
            $config['orig_width']       = $orig_width;
            $config['orig_height']      = $orig_height;
            //helper untuk jadiin slug dan penomoran file untuk menghindari duplikasi file
            $this->load->helper('slug');
            $new_name                   = convert_to_slug($file_name);
            //path upload file baru (hasil crop)
            $config['new_image']        = $upload_path . $new_name .'-crop-'. date('YmdHis') . '.' . $file_extension;

            $this->image_lib->initialize($config);
            //process thumb and reset the original with and height
            //sumber https://adisembiring.wordpress.com/2009/12/25/upload-dan-crop-image-menggunakan-jcrop-dan-ci/
            $this->image_lib->image_process_gd('thumb', $orig_width, $orig_height);            

            //nama file setelah di crop
            $data['crop_result'] = pathinfo($config['new_image'], PATHINFO_BASENAME );                       
            $this->load->view('back/layouts/media/image_crop', $data);
        }else{
            show_404();
        }
    }

    function delete_image() {
        $file_with_web_path     = $this->input->post('delete_image');
        $file_name              = pathinfo($file_with_web_path, PATHINFO_BASENAME);
        $file_server_with_path  = './uploads/' . $file_name;
        //hapus file
        unlink($file_server_with_path);
    }

    function add(){
        //rule type pada function ini: Read
        $this->rule->type('R');
        
        //title
        $this->layout->set_title('Tambah Media');

        //library Smart Upload
        $this->load->library('dragdropimage');

        //breadcrumb/untuk navigasi
        $this->breadcrumb->clear();
        $this->breadcrumb->add_crumb('Dashboard', site_url('admin'));
        $this->breadcrumb->add_crumb('Media', site_url('media/add#'));
        $this->breadcrumb->add_crumb('Pustaka Media', site_url('media'));
        $this->breadcrumb->add_crumb('Tambah Baru');

        //judul besar
        $data['primary_title'] = '<i class="fa fa-camera"></i> Media';
        $data['sub_primary_title'] = 'Pengaturan media Notoboso';

        $this->layout->theme('back','media/add', $data);
    }
}
